package com.zjj.learn.designmodel.pattern.singleton.demo7;

import java.io.*;

/** 测试通过序列化和反序列化破坏单例模式
 * *** @author zjj_admin
 * *** 2022/4/5 16:43
 */
public class ClientTest7 {

    public static void main(String[] args) throws Exception {
       // writeObjectToFile();
        readObjectFromFile();
        readObjectFromFile();
    }



    //从文件中读取文件
    public static void readObjectFromFile() throws Exception {
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("C:\\Users\\zjj_admin\\Desktop\\aa.txt"));
        Singleton singleton = (Singleton) inputStream.readObject();
        System.out.println(singleton);
        inputStream.close();
    }
    //将对象写入到文件
    public static void writeObjectToFile() throws Exception {
        Singleton instance = Singleton.getInstance();
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("C:\\Users\\zjj_admin\\Desktop\\aa.txt"));
        outputStream.writeObject(instance);
        outputStream.close();
    }
}
